package cn.tedu.micro.demo.device;

import cn.tedu.micro.demo.common.pojo.po.GunInfoPO;
import cn.tedu.micro.demo.device.mapper.GunInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.List;

/**
 * 这是一个专门为device设备 项目准备容器单元测试类
 * 这个测试类能够正常加载容器 并且拿到容器bean对象进行测试满足条件
 * 1. 所在的包要和启动类同名
 * 2. 运行单元测试方法必须加载启动类DeviceApplication 使用@SpringBootTest
 */
@SpringBootTest
public class DeviceTestMapper {
    //测试任何容器中的bean对象
    @Autowired
    private GunInfoMapper gunInfoMapper;
    /**
     * 新增一条gun_info表格的行数 其中id是自增,所以默认null
     * insert into gun_info (id,status,type,create_time,update_time) values (?,?,?,?,?,?)
     */
    @Test
    public void saveOne(){
        //1.insert(GunInfoPO entity); 拼接insert语句 使用传递的po对象的非空属性 写sql
        GunInfoPO poParam=new GunInfoPO();
        poParam.setStatus(1);
        poParam.setType(1);
        //poParam.setCreateTime(new Date());
        //createTime updateTime 保持空
        //insert into gun_info (status,type) values (?,?) id自增 时间默认值
        gunInfoMapper.insert(poParam);
    }
    @Test
    public void saveBatch(){
        //1.直接新增多条 有多少增多少
        //insert into gun_info (id,status,type,create_time,update_time)
        //values (?,?,?,?,?,?),(?,?,?,?,?,?)
        List<GunInfoPO> poParams=List.of(
                new GunInfoPO(null,2,2,null,null),
                new GunInfoPO(null,3,3,null,null),
                new GunInfoPO(null,4,4,null,null),
                new GunInfoPO(null,5,5,null,null),
                new GunInfoPO(null,6,6,null,null),
                new GunInfoPO(null,7,7,null,null),
                new GunInfoPO(null,8,8,null,null),
                new GunInfoPO(null,9,9,null,null),
                new GunInfoPO(null,10,10,null,null),
                new GunInfoPO(null,11,11,null,null));
        //insert into gun_info (status,type)
        //values (?,?),(?,?)
        //gunInfoMapper.insert(poParams);
        //2.多条新增,可以批量执行,如果一次性执行新增数据量过大 支持批量执行 每次拿到一个连接 只新增批量list一部分
        gunInfoMapper.insert(poParams,5);//10条数据用两次连接 每次新增5条
    }

    /**
     * 使用id查询一个表格记录 封装PO返回
     */
    @Test
    public void selectById(){
        //1.select id,status,type,create_time,update_time
        //from where id=?
        GunInfoPO gunInfoPO = gunInfoMapper.selectById(1);//使用主键查询 一定是PO类中添加@TableId的字段
        System.out.println(gunInfoPO);
    }
    /**
     * 使用其他条件查询
     */
    @Test
    public void selectByOther(){
        //1.select id,status,type,create_time,update_time from gun_info where type=?;
        //使用多种条件 意味着where不一样,mybatis-plus给我们提供了查询拼接where的对象
        QueryWrapper queryWrapper=new QueryWrapper();//queryWrapper==>where XXXXXX
        //2.使用查询条件,要确保 查询的是一条selectOne(QueryWrapper)还是多条selectList(QueryWrapper)
        //2.1 where id=1
        /*queryWrapper.eq("id",1);
        GunInfoPO gunInfoPO = gunInfoMapper.selectOne(queryWrapper);*/
        //2.2 where type=2
        queryWrapper.eq("type",2);
        List poList = gunInfoMapper.selectList(queryWrapper);
        System.out.println(poList);
    }
    //where条件拼接测试 > >= < <= between
    @Test
    public void selectByGtLtEqual(){
        QueryWrapper queryWrapper=new QueryWrapper();
        //where type>2;
        //queryWrapper.gt("type",2);
        //where type<2
        //queryWrapper.lt("type",2);
        //where type>=2
        //queryWrapper.ge("type",2);
        //where type<=2
        //queryWrapper.le("type",2);
        //where type between 2 and 5
        queryWrapper.between("type",2,5);
        gunInfoMapper.selectList(queryWrapper);
    }
    /**
     * 查询 where id in (?,?,?)
     */
    @Test
    public void selectIn(){
        //1.select * from gun_info where id in (?,?,?)
        QueryWrapper queryWrapper=new QueryWrapper();
        //拼queryWrapper where条件 执行selectList
        //1.1 使用list做参数 jdk 17 才能用List.of
        //List<Integer> ids=List.of(1,2,3,4);
        //queryWrapper.in("id",ids);
        //1.2 使用可变参数传参
        queryWrapper.in("id",1,2,3,4);
        gunInfoMapper.selectList(queryWrapper);
    }
    //TODO like测试略 ,因为需要字符串数据字段
    /**
     * 多条件拼接 AND
     */
    @Test
    public void selectAnd(){
        //where id in (1,2,3,4) and type=2
        //使用同一个queryWrapper 一个一个条件单独拼接
        //where id in (1,2,3,4)
        QueryWrapper queryWrapper=new QueryWrapper();
        List<Integer> ids=List.of(1,2,3,4);
        queryWrapper.in("id",ids);
        //where type=2
        queryWrapper.eq("type",2);
        gunInfoMapper.selectList(queryWrapper);
    }
    /**
     *  where id in(1,2,3,4,5) or type=2;
     */
    @Test
    public void selectOr(){
        QueryWrapper queryWrapper=new QueryWrapper();
        //where id in (1,2,3,4)
        List<Integer> ids=List.of(1,2,3,4);
        queryWrapper.in("id",ids);
        //or单独调用方法连接
        queryWrapper.or();
        //where type=2
        queryWrapper.eq("type",2);
        gunInfoMapper.selectList(queryWrapper);
    }

    //阶段性练习
    @Test
    public void practiceAnswer(){
        QueryWrapper queryWrapper=new QueryWrapper();
        //1.select id,status,type,create_time,update_time from gun_info
        //where update_time>2019年1月1日0点;
        //2019-01-01 00:00:00 字符串会自动转化成时间类型
        /*queryWrapper.gt("create_time","2026-01-01 00:00:00");
        gunInfoMapper.selectList(queryWrapper);*/
        //2.where in (1,2,3) AND type = 2 OR create_time>2019年1月1日0点;
        List<Integer> ids=List.of(1,2,3);
        queryWrapper.in("id",ids);//where id in (1,2,3)
        queryWrapper.eq("type",2);//where id in (1,2,3) and type=2
        queryWrapper.or();
        queryWrapper.gt("create_time","2019-01-01 00:00:00");
        //where id in (1,2,3) and type=2 or create_time>2019年1月1日0点;
        gunInfoMapper.selectList(queryWrapper);
    }
    //根据id删除行
    @Test
    public void deleteById(){
        //1.delete from gun_info where id=?
        //int delete = gunInfoMapper.deleteById(1);//返回值表示的是删除影响的数据个数
        //2.自定义查询条件 where 和select方法完全一样的
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",1);
        gunInfoMapper.delete(queryWrapper);
    }
    //更新
    //update gun_info set type=10 where id=1;
    @Test
    public void updateById(){
        //1. 自己拼接 set的属性字段 和值 并且提供where条件
        UpdateWrapper updateWrapper=new UpdateWrapper();
        //updateWrapper where条件拼接 和queryWrapper完全一样
        updateWrapper.eq("id",2);
        updateWrapper.set("status",0);
        //updateWrapper 可以手动拼接想要执行更新set关键字
        //updateWrapper.set("type",10);//set type=10
        //gunInfoMapper.update(updateWrapper);
        //2.还可以使用一个GunInfoPO对象的非空属性 让底层框架自动拼接set
        GunInfoPO poParam=new GunInfoPO();
        poParam.setType(11);//set type=?
        poParam.setUpdateTime(new Date()); //update_time=?
        gunInfoMapper.update(poParam,updateWrapper);
    }
    //更新 set中数据是计算结果不是直接赋值
    @Test
    public void updateTypeIncr(){
        //1.update gun_info set type=type+5 where id=2;
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",2);
        //set type=type+5;
        updateWrapper.setSql("type=type+5");
        gunInfoMapper.update(updateWrapper);
    }
    //排序 属于 查询搜索sql中常见的关键字 所以 封装在QueryWrapper
    @Test
    public void selectOrder(){
        //select id,status,type,create_time,update_time where type>1 order by create_time desc
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.gt("type",1);
        //拼接排序的关键字
        queryWrapper.orderByDesc("update_time");
        gunInfoMapper.selectList(queryWrapper);
    }
    //分页查询
    @Test
    public void selectPage(){
        //1. select * from gun_info where type>1 limit 5,5;
        //考虑where条件
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.gt("type",1);
        //拼接limit关键字
        /*queryWrapper.last("limit 10,5");
        gunInfoMapper.selectList(queryWrapper);*/
        //2.mybatis-plus支持直接封装分页对象数据做查询
        /*Page<GunInfoPO> page=new Page(1,5);//不用计算from size 直接使用分页pageNum pageSize
        IPage<GunInfoPO> gunPage = gunInfoMapper.selectPage(page, queryWrapper);*/
    }

}

